<template>
	<div class="system-dic-container layout-padding">
		<el-card shadow="hover" class="layout-padding-auto">
			<div class="system-user-search mb15">
				<el-input size="default" placeholder="请输入渠道名称" style="max-width: 180px" v-model="state.tableData.param.param.cname.val"> </el-input>
				<el-select v-model="state.tableData.param.param.plat_id.val" filterable placeholder="请选择所属平台" clearable size="default" style="width: 180px;">
					<el-option v-for="(item,index) in state.platsData.palt" :key="item.id" :label="item.cname" :value="item.id"></el-option>
				</el-select>
				<el-button size="default" type="primary"  @click="seachClick">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
				<el-button size="default" type="success"  v-auth="'/group/platform/add'"  @click="onOpenAddCompany('add')">
					<el-icon>
						<ele-FolderAdd />
					</el-icon>
					新增渠道
				</el-button>
			</div>
			<el-table 
				:data="state.tableData.data" 
				v-loading="state.tableData.loading" 
				row-key="id"
				:tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
				style="width: 100%">
				<el-table-column prop="id" label="ID" show-overflow-tooltip width="80"></el-table-column>
				<el-table-column prop="cname" label="渠道名称" show-overflow-tooltip></el-table-column>
				<el-table-column prop="off_id" label="渠道ID" show-overflow-tooltip width="150"></el-table-column>

				
				<el-table-column label="渠道图片" width="120" align="center">
					<template #default="scope">
						<el-image style="width: 80px;height: 40px;background: #000;border-radius: 5px;" :src="state.imgUrl + scope.row.img" fit="contain" :preview-src-list="[state.imgUrl + scope.row.img]"	preview-teleported	>
							<template #error>
								<div class="image-slot">
									<el-image style="width: 80px;height: 40px" :src="chatHealth" fit="contain" ></el-image>
								</div>
							</template>
						</el-image>
					</template>
				</el-table-column>
				<el-table-column label="渠道评级" width="200">
					<template #default="scope">
						<el-rate
							v-model="scope.row.star_num"
							disabled
							text-color="#ff9900"
						/>
					</template>
				</el-table-column>
				<el-table-column prop="plat_name"  label="所属平台"  show-overflow-tooltip> 
				</el-table-column>
				<el-table-column prop="status" label="渠道状态" width="120">
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.status == 1">启用</el-tag>
						<el-tag type="info" v-else>禁用</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="创建时间"  width="200">
					<template #default="scope">
						<span>{{ dateFormatYMDHMS(scope.row.create_time * 1000)}}</span>
                    </template>
				</el-table-column>
				<el-table-column label="操作" width="150">
					<template #default="scope">
						<el-button size="small" text type="primary" v-auth="'/system/plat/conf'"  @click="onOpenEditConfig(scope.row)">配置</el-button>
						<el-button size="small" text type="primary" v-auth="'/group/platform/edit'"  @click="onOpenEditCompany('edit', scope.row)">修改</el-button>
						<el-button size="small" text type="primary" v-auth="'/group/platform/del'"  @click="onRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<el-pagination
				@size-change="onHandleSizeChange"
				@current-change="onHandleCurrentChange"
				class="mt15"
				:pager-count="5"
				:page-sizes="[10, 20, 30]"
				v-model:current-page="state.tableData.param.page"
				background
				v-model:page-size="state.tableData.param.limit"
				layout="total, sizes, prev, pager, next, jumper"
				:total="state.tableData.total"
			>
			</el-pagination>
		</el-card>
		<platformDialog ref="platformDialogRef" @refresh="getTableData()" />
		<configDialog ref="configDialogRef"/>
	</div>
</template>

<script setup name="platform">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { platformApi } from '/@/api/platform/index';
import { commApi } from '/@/api/comm/index';
import commonFunction from '/@/utils/commonFunction';
// 定义变量内容
const { dateFormatYMDHMS } = commonFunction();
// 引入组件
const platformDialog = defineAsyncComponent(() => import('./dialog.vue'));
const configDialog = defineAsyncComponent(() => import('./dialogConfig.vue'));
import chatHealth from '/@/assets/baierror.png'
const paramOp = {
	cname:'%%'
}
// 定义变量内容
const platformDialogRef = ref();
const configDialogRef = ref();
const state = reactive({
	imgUrl:import.meta.env.VITE_IMG_URL,
	platsData:{
		palt:[]
	},
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			param:{
				cname:{},
				plat_id:{},
				outside:{
					val:"0",
					op:'='
				},
			},
			page: 1,
			limit: 10,
		},
	},
});
const getPlats = () => {
	commApi().getPlats().then(res=>{
		state.platsData.palt = res.data.palt
	})
}
// 初始化表格数据
const getTableData = () => {
	state.tableData.loading = true;
	for (let key in state.tableData.param.param) {
		if (state.tableData.param.param[key].val && state.tableData.param.param[key].val !== '') {
			if(!state.tableData.param.param[key].op){
				state.tableData.param.param[key].op = paramOp[key] || '='
			}
		}else{
			state.tableData.param.param[key] = {}
		}
	}
    platformApi().index(state.tableData.param).then(res=>{
        state.tableData.data = res.data.list;
        state.tableData.total = res.data.total;
        state.tableData.loading = false;
    })
};
// 打开新增渠道弹窗
const onOpenAddCompany = (type) => {
	platformDialogRef.value.openDialog(type);
};
// 打开修改渠道弹窗
const onOpenEditCompany = (type, row) => {
	platformDialogRef.value.openDialog(type, row);
};
//打开配置
const onOpenEditConfig = (row)=>{
	configDialogRef.value.openDialog(row);
}
// 删除渠道
const onRowDel = (row) => {
	ElMessageBox.confirm(`此操作将永久删除渠道名称：“${row.cname}”，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	})
		.then(() => {
			platformApi().del({ids:row.id}).then(res=>{
                getTableData();
                ElMessage.success('删除成功');
            })
		})
		.catch(() => {});
};
const seachClick = () =>{
	onHandleCurrentChange(1)
}
// 分页改变
const onHandleSizeChange = (val) => {
	state.tableData.param.limit = val;
	getTableData();
};
// 分页改变
const onHandleCurrentChange = (val) => {
	state.tableData.param.page = val;
	getTableData();
};
// 页面加载时
onMounted(() => {
	getPlats()
	getTableData();
});
</script>
